package com.zy.ivideo.service;

import com.zy.ivideo.entity.BankCardEntity;
import com.zy.ivideo.entity.ResponseEntity;
import com.zy.ivideo.repository.BankCardRepository;
import com.zy.ivideo.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import java.util.List;

@Service
public class BankCardService {
    @Autowired
    BankCardRepository repository;

    @Autowired
    EntityManager entityManager;

    /**
     * 添加银行卡
     * @param entity
     * @return
     */
    public ResponseEntity<BankCardEntity> addBankCard(BankCardEntity entity){
        BankCardEntity result = repository.save(entity);
        if (result!=null){
            return ResponseUtils.success(result);
        }

        return ResponseUtils.failed("添加银行卡失败");
    }

    /**
     * 修改银行卡信息
     * @param entity
     * @return
     */
    public ResponseEntity<BankCardEntity> modifyBankCard(BankCardEntity entity){
        BankCardEntity result = repository.save(entity);
        if (result!=null){
            return ResponseUtils.success(result);
        }
        return ResponseUtils.failed("修改银行卡信息");
    }

    /**
     * 删除银行卡
     * @param id
     * @return
     */
    public ResponseEntity<Boolean> removeBankCard(Long id){
        try
        {
            repository.deleteById(id);
            return ResponseUtils.success(true);
        }
        catch (Exception ex){
            return ResponseUtils.failed("删除银行卡出错，error:"+ex.getMessage());
        }
    }

    /**
     * 根据账号id获取银行卡信息
     * @param accountId
     * @return
     */
    public ResponseEntity<List<BankCardEntity>> findBankCardByAccountId(Long accountId){
        List<BankCardEntity> result = repository.findBankCardEntitiesByAccountid(accountId);
        if (result!=null&&result.size()>0){
            return ResponseUtils.success(result);
        }
        return ResponseUtils.failed("没有获取到银行卡信息");
    }

    /**
     * 根据账号id删除银行卡信息
     * @param accountId 账号id
     * @return
     */
    public ResponseEntity<Boolean> deleteByAccountId(Long accountId){
        int result = repository.deleteByAccountid(accountId);
        if (result>0){
            return ResponseUtils.success(true);
        }
        return ResponseUtils.failed("根据账号id删除银行卡信息");
    }
}
